const app = getApp();
const utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShareVersion: false,//是否为共享版
    gradeList: [], //共享股东等级列表
    leval: '',
    percentage: '',
    number: '',
    gradeid: 0,
    activeLeval: false, //增加股东等级弹窗
    broType: 0  //佣金方式: 0 按实付金额, 1 按利润
  },
  // 获取共享股东等级列表
  getgradeList: function() {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetShareGradeList?createUid=' + app.globalData.userInfo.Uid,
      success: function(res) {
        if (res.data.Code == 0) {
          let gradeList = res.data.Data;
          for (let i = 0; i < gradeList.length; i++) {
            let count = 1;
            let gift = [];
            let cardItemList = gradeList[i].CardItemList;
            let couponTypeList = gradeList[i].CouponTypeList;
            if (gradeList[i].Card != null) {
              let content = count + "、" + (gradeList[i].Card.Discount * 0.1).toFixed(1) + "折扣卡1张";
              gift.push(content);
              count++;
            }
            if (cardItemList.length > 0) {
              for (let j = 0; j < cardItemList.length; j++) {
                let content = count+ "、"+ cardItemList[j].Name + "1张";
                count++;
                gift.push(content);
              }
            }
            if (couponTypeList.length > 0) {
              for (let k = 0; k < couponTypeList.length; k++) {
                let content = '';
                if (couponTypeList[k].money>0){
                  content = count + "、满" + couponTypeList[k].orderamountlower + "元减" + couponTypeList[k].money + "元代金券" + couponTypeList[k].Counts + "张";
                } else if (couponTypeList[k].name != ''){
                  content = count + "、" + couponTypeList[k].name + "赠品券" + couponTypeList[k].Counts + "张";
                }
                else if (couponTypeList[k].extcode1 != '') {
                  content = count + "、买" + couponTypeList[k].extcode1 + "送" + couponTypeList[k].extcode2 + "赠品券" + couponTypeList[k].Counts + "张";
                }else{
                  content = count + "、送" + couponTypeList[k].extcode2 + "赠品券" + couponTypeList[k].Counts + "张";
                }
                count++;
                gift.push(content);
              }
            }
            gradeList[i].gifts = gift;
          }
          that.setData({
            gradeList: gradeList
          })
        } else {
          utils.showModal(res.data.Message);  
        }
      }
    })
  },
  // 查看股东数
  toHolderlist: function(e) {
    let gradeid = e.currentTarget.dataset.gradeid;
    let listType = e.currentTarget.dataset.listtype;
    let count = e.currentTarget.dataset.count;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/merchant/share/holderList/index?gradeid=' + 　gradeid + '&listtype=' + listType + '&count=' + count + '&name=' + name,
    })
  },
  // 提现申请记录
  toTixianRequest: function() {
    wx.navigateTo({
      url: '/merchant/share/tixianRequest/index',
    })
  },
  bindleval: function(e) {
    this.setData({
      leval: e.detail.value
    })
  },
  bindpercentage: function(e) {
    this.setData({
      percentage: e.detail.value
    })
  },
  bindnumber: function(e) {
    this.setData({
      number: e.detail.value
    })
  },
  //监听用户滑动页面事件
  onPageScroll: function (e) {
    let top = parseInt(e.scrollTop);
    this.setData({
      top: top
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getgradeList();
  },
  // 优惠券发放（核销）量
  toUseRecord: function(e){
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let listtype = e.currentTarget.dataset.listtype;
    wx.navigateTo({
      url: '/merchant/merchants/sendAndCheckRecords/index?coupontypeid=' + coupontypeid + '&listtype=' + listtype,
    })
  },
  // 查看消费记录
  toRecords: function() {
    wx.navigateTo({
      url: '/merchant/merchants/records/index',
    })
  },
  // 新增项目
  addProject: function() {
    wx.navigateTo({
      url: '/merchant/merchants/addProject/index',
    })
  },
  // 新增卡券权益（大礼包）
  toAddProjectShare: function(e) {
    let gradeid = e.currentTarget.dataset.gradeid;
    let count = e.currentTarget.dataset.count;
    wx.navigateTo({
      url: '/merchant/share/holderCoupon/index?gradeid=' + gradeid + '&count=' + count,
    })
  },
  saveLeval: function() {
    let that = this;
    if (!that.data.leval) {
      utils.showToast('请输入等级名称'); return;
    }
    if (!that.data.percentage || that.data.percentage > 100) {
      utils.showToast('请输入0到100之间的比例'); return;
    }
    if (!that.data.number) {
      utils.showToast('请输入礼包数量'); return;
    }
    wx.request({
      url: app.globalData.host + '/api/Shareholder/AddUpdateShareGrade',
      data: {
        id: that.data.gradeid,
        uid: app.globalData.userInfo.Uid,
        name: that.data.leval,
        packageCount: that.data.number,
        Discount: that.data.percentage,
        broType: that.data.broType
      },
      method: 'POST',
      success: function(res) {
        if (res.data.Code == 0) {     
          utils.showToast('添加成功')
          that.setData({
            tagindex: 2,
            activeLeval: !that.data.activeLeval
          })
          that.getgradeList();
        } else {
          utils.showModal(res.data.Message);        
        }
      }
    })
  },
  // 新增弹窗
  showLeval: function(e) {    
    let gradeid = e.currentTarget.dataset.gradeid;
    if (gradeid > 0) {
      this.getGrade(gradeid);
      return;
    }
    this.setData({
      gradeid: 0,
      leval: '',
      percentage: '',
      number: '',
      broType: 0,
      activeLeval: !this.data.activeLeval
    })
  },
  //关闭弹框
  closeLeval: function() {
    this.setData({
      activeLeval: !this.data.activeLeval
    })
  },
  // 删除等级
  delLeval: function(e) {
    let that = this;
    let gradeid = e.currentTarget.dataset.gradeid;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function(resCrm){
        if (resCrm.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Shareholder/DeleteShareGrade?id=' + gradeid,            
            success: function(res) {
              if (res.data.Code != 0) {
                utils.showModal(res.data.Message); return;
              } 
              wx.showToast({title: '删除成功'})              
              that.getgradeList();
            }
          })        
        } 
      }
    })
    
  },
  // 单个共享股东等级信息
  getGrade: function(gradeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetShareGrade?id=' + gradeid,
      success: function(res) {
        let grade = res.data.Data.info;
        if (res.data.Code == 0) {
          that.setData({
            leval: grade.Name,
            percentage: grade.Discount,
            number: grade.PackageCount,
            broType: grade.BrokerageType,
            activeLeval: true
          })
        } else {
          utils.showModal(res.data.Message);     
        }
      }
    })
  },
  //佣金方式
  bindBroType: function(e){
    this.setData({
      broType: e.currentTarget.dataset.brotype
    })
  }
})